package mi.payment.transaction.refund;

import android.media.AudioManager;
import android.media.ToneGenerator;
import android.os.Bundle;
import android.support.design.card.MaterialCardView;
import android.view.View;
import android.widget.TextView;

import lainey.ui.utils.LogUtil;
import lainey.ui.widget.TitleView;
import mi.payment.R;
import mi.payment.constants.Configs;
import mi.payment.constants.Params;
import mi.payment.db.dao.UserInfo;
import mi.payment.settings.TransactionOtherOperation;
import mi.payment.settings.TransactionSwitchOperation;
import mi.payment.settings.TransactionSwitchSetting;
import mi.payment.ui.base.ParentActivity;
import mi.payment.ui.base.PasswordInputView;
import mi.payment.ui.base.PasswordKeyboard;
import sunmi.payment.L3.L3BroadcastHelper;
import sunmi.payment.L3.L3Constant;
import sunmi.payment.L3.L3ErrorCode;

/**
 * 退货模块 -- 输入主管密码界面
 */
public class ReturnGoodsInputAdminPwd extends ParentActivity {

    private MaterialCardView mCardBank;
    private MaterialCardView mCardCode;
    private MaterialCardView mCardWx;
    private MaterialCardView mCardAli;
    private MaterialCardView mCardUnion;
    private TextView mTvBank;
    private TextView mTvCode;
    private TextView mTvWx;
    private TextView mTvAli;
    private TextView mTvUnion;

    // 当前选择的退货支付类型；0位银行卡，1位支付宝，2位微信，3银联扫码，5,二维码
    private int mSelectPayType = 0;
    private ToneGenerator mToneGenerator;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.transaction_refund_activity_input_admin_password);
        initView();
        fillData();
        initToneGenerator();

        visibleViews();
        updatePayMode();
    }

    private void initView() {
        TitleView titleView = findViewById(R.id.title_view);
        titleView.setCenterText(R.string.refund);
        titleView.setLeftImageOnClickListener(
                v -> onBackPressed()
        );

        mCardBank = findViewById(R.id.card_pay_bank);
        mCardCode = findViewById(R.id.card_pay_qr_code);
        mCardWx = findViewById(R.id.card_pay_wx);
        mCardAli = findViewById(R.id.card_pay_ali);
        mCardUnion = findViewById(R.id.card_pay_union);

        mTvBank = findViewById(R.id.tv_pay_bank);
        mTvCode = findViewById(R.id.tv_pay_qr_code);
        mTvWx = findViewById(R.id.tv_pay_wx);
        mTvAli = findViewById(R.id.tv_pay_ali);
        mTvUnion = findViewById(R.id.tv_pay_union);

        PasswordKeyboard passwordKeyboard = findViewById(R.id.passwordKeyboard);
        PasswordInputView passwordInputView = findViewById(R.id.passwordInputView);

        mCardBank.setOnClickListener(this);
        mCardCode.setOnClickListener(this);
        mCardWx.setOnClickListener(this);
        mCardAli.setOnClickListener(this);
        mCardUnion.setOnClickListener(this);
        findViewById(R.id.mb_ok).setOnClickListener(this);

        View okView = findViewById(R.id.view_ok);
        TextView hint = findViewById(R.id.tv_hint);
        // 撤销退货类交易是否输入主管密码
        boolean inputDirectorPsd = TransactionOtherOperation.getInstance().getTransactionOtherSettings().getInputDirectorPsd();
        if (inputDirectorPsd) {
            passwordKeyboard.setVisibility(View.VISIBLE);
            passwordInputView.setVisibility(View.VISIBLE);
            okView.setVisibility(View.GONE);
            hint.setVisibility(View.VISIBLE);
        } else {
            passwordKeyboard.setVisibility(View.GONE);
            passwordInputView.setVisibility(View.GONE);
            okView.setVisibility(View.VISIBLE);
            hint.setVisibility(View.GONE);
        }

        passwordKeyboard.setKeyClickCallback(
                new PasswordKeyboard.KeyClickCallback() {

                    @Override
                    public void onNumClick(int keyNum) {
                        sound();
                        passwordInputView.addText(keyNum + "");
                    }

                    @Override
                    public void onDelClick() {
                        passwordInputView.delLast();
                    }

                    @Override
                    public void onEnterClick() {
                        String password = passwordInputView.getPassword();
                        UserInfo user = mUserInfoModel.login("00", password);
                        boolean isRight = user != null && user.getUPwd().equals(password);
                        if (isRight) {
                            clickOK();
                        } else {
                            passwordInputView.clearText();
                            showToast(R.string.error_password);
                        }
                    }

                }
        );
    }

    private void initToneGenerator() {
        try {
            mToneGenerator = new ToneGenerator(AudioManager.STREAM_SYSTEM, ToneGenerator.MAX_VOLUME);
        } catch (Exception e) {
            LogUtil.e(Configs.TAG, "打开ToneGenerator失败");
            e.printStackTrace();
        }
    }

    private void fillData() {
        // L3调用，选中相应的标签
        if (Params.sL3Params == null) return;
        switch (Params.sL3Params.paymentType) {
            case L3Constant.PayType.BARCODE_TYPE_AliPay_QR:
            case L3Constant.PayType.BARCODE_TYPE_AliPay_SCAN:
                mSelectPayType = 1;
                break;
            case L3Constant.PayType.BARCODE_TYPE_WeChat_QR:
            case L3Constant.PayType.BARCODE_TYPE_WeChat_SCAN:
                mSelectPayType = 2;
                break;
            case L3Constant.PayType.BARCODE_TYPE_UNION_QR:
            case L3Constant.PayType.BARCODE_TYPE_UNION_SCAN:
                mSelectPayType = 3;
                break;
        }
    }

    /**
     * 设置需要显示的View以及默认值
     */
    private void visibleViews() {
        // 是否支持二维码退货功能
        if (Configs.isRefundSupportQRCode) {
            LogUtil.e(Configs.TAG, "支持二维码退货功能");
        } else {
            mCardWx.setVisibility(View.VISIBLE);
            mCardWx.setVisibility(View.GONE);
            mCardAli.setVisibility(View.GONE);
            mCardUnion.setVisibility(View.GONE);
            return;
        }
        if (Configs.isRefundMergeQRCode) {
            // 合并二维码
            mCardCode.setVisibility(View.VISIBLE);
            mCardWx.setVisibility(View.GONE);
            mCardAli.setVisibility(View.GONE);
            mCardUnion.setVisibility(View.GONE);
        } else {
            mCardCode.setVisibility(View.GONE);
            TransactionSwitchSetting setting = TransactionSwitchOperation.getInstance().getTransactionSwitchSetting();
            // 是否支持支付宝
            boolean support = setting.isUseAliPayQR() || setting.isUseAliPayScan();
            if (Configs.isRefundSupportAli && support) {
                mCardAli.setVisibility(View.VISIBLE);
            } else {
                mCardAli.setVisibility(View.GONE);
            }
            // 是否支持微信
            support = setting.isUseWeChatQR() || setting.isUseWeChatScan();
            if (Configs.isRefundSupportWeChat && support) {
                mCardWx.setVisibility(View.VISIBLE);
            } else {
                mCardWx.setVisibility(View.GONE);
            }
            // 是否支持银联
            support = setting.isUseUnionPayQR() || setting.isUseUnionPayScan();
            if (Configs.isRefundSupportUnion && support) {
                mCardUnion.setVisibility(View.VISIBLE);
            } else {
                mCardUnion.setVisibility(View.GONE);
            }
        }
    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
        L3BroadcastHelper.sendFailedBroadcast(mContext, L3ErrorCode.ERROR_TRADE_CANCEL);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mToneGenerator != null) {
            mToneGenerator.release();
        }
    }

    @Override
    public void onClick(View v) {
        final int id = v.getId();
        switch (id) {
            case R.id.card_pay_bank:
                mSelectPayType = 0;
                updatePayMode();
                break;
            case R.id.card_pay_ali:
                mSelectPayType = 1;
                updatePayMode();
                break;
            case R.id.card_pay_wx:
                mSelectPayType = 2;
                updatePayMode();
                break;
            case R.id.card_pay_union:
                mSelectPayType = 3;
                updatePayMode();
                break;
            case R.id.card_pay_qr_code:
                mSelectPayType = 5;
                updatePayMode();
                break;
            case R.id.mb_ok:
                clickOK();
                break;
        }
    }

    private void clickOK() {
        switch (mSelectPayType) {
            case 0:
                ReturnGoodsSwingCardActivity.startAction(this);
                finish();
                break;
            case 1:
            case 2:
            case 3:
            case 4:
            case 5:
                ReturnGoodsScanActivity.startAction(this, mSelectPayType);
                finish();
                break;
            default:
                ReturnGoodsSwingCardActivity.startAction(this);
                finish();
                break;
        }
    }

    /**
     * 提示音
     */
    private void sound() {
        if (mToneGenerator != null) {
            mToneGenerator.startTone(ToneGenerator.TONE_PROP_BEEP);
        }
    }

    private void updatePayMode() {
        int colorPrimary = getResources().getColor(R.color.colorPrimary);
        int colorTextTitle = getResources().getColor(R.color.colorTextTitle);
        int colorLineColor = getResources().getColor(R.color.colorLineColor);
        switch (mSelectPayType) {
            case 0:
                mCardBank.setStrokeColor(colorPrimary);
                mCardCode.setStrokeColor(colorLineColor);
                mCardWx.setStrokeColor(colorLineColor);
                mCardAli.setStrokeColor(colorLineColor);
                mCardUnion.setStrokeColor(colorLineColor);

                mTvBank.setTextColor(colorPrimary);
                mTvCode.setTextColor(colorTextTitle);
                mTvWx.setTextColor(colorTextTitle);
                mTvAli.setTextColor(colorTextTitle);
                mTvUnion.setTextColor(colorTextTitle);
                break;
            case 1:
                mCardBank.setStrokeColor(colorLineColor);
                mCardCode.setStrokeColor(colorLineColor);
                mCardWx.setStrokeColor(colorLineColor);
                mCardAli.setStrokeColor(colorPrimary);
                mCardUnion.setStrokeColor(colorLineColor);

                mTvBank.setTextColor(colorTextTitle);
                mTvCode.setTextColor(colorTextTitle);
                mTvWx.setTextColor(colorTextTitle);
                mTvAli.setTextColor(colorPrimary);
                mTvUnion.setTextColor(colorTextTitle);
                break;
            case 2:
                mCardBank.setStrokeColor(colorLineColor);
                mCardCode.setStrokeColor(colorLineColor);
                mCardWx.setStrokeColor(colorPrimary);
                mCardAli.setStrokeColor(colorLineColor);
                mCardUnion.setStrokeColor(colorLineColor);

                mTvBank.setTextColor(colorTextTitle);
                mTvCode.setTextColor(colorTextTitle);
                mTvWx.setTextColor(colorPrimary);
                mTvAli.setTextColor(colorTextTitle);
                mTvUnion.setTextColor(colorTextTitle);
                break;
            case 3:
                mCardBank.setStrokeColor(colorLineColor);
                mCardCode.setStrokeColor(colorLineColor);
                mCardWx.setStrokeColor(colorLineColor);
                mCardAli.setStrokeColor(colorLineColor);
                mCardUnion.setStrokeColor(colorPrimary);

                mTvBank.setTextColor(colorLineColor);
                mTvCode.setTextColor(colorTextTitle);
                mTvWx.setTextColor(colorTextTitle);
                mTvAli.setTextColor(colorTextTitle);
                mTvUnion.setTextColor(colorPrimary);
                break;
            case 5:
                mCardBank.setStrokeColor(colorLineColor);
                mCardCode.setStrokeColor(colorPrimary);
                mCardWx.setStrokeColor(colorLineColor);
                mCardAli.setStrokeColor(colorLineColor);
                mCardUnion.setStrokeColor(colorLineColor);

                mTvBank.setTextColor(colorLineColor);
                mTvCode.setTextColor(colorPrimary);
                mTvWx.setTextColor(colorTextTitle);
                mTvAli.setTextColor(colorTextTitle);
                mTvUnion.setTextColor(colorTextTitle);
                break;
            default:
                break;
        }
    }


}
